//
//  SCMenuView.m
//  Shuaiche
//
//  Created by dangshuai on 2019/4/24.
//  Copyright © 2019 zzs. All rights reserved.
//

#import "SCMenuView.h"
#import "UIView+Line.h"
@implementation SCMenuView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

+ (Class)layerClass {
    return [CAShapeLayer class];
}

- (instancetype)init {
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        CAShapeLayer *shape = (CAShapeLayer *)self.layer;
        shape.fillColor = [UIColor whiteColor].CGColor;
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat w =  CGRectGetWidth(self.bounds);
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 10, w, CGRectGetHeight(self.bounds) - 10) byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(4, 4)];
    
    UIBezierPath *arrPath = [UIBezierPath bezierPath];
    [arrPath moveToPoint:CGPointMake(w, 0)];
    [arrPath addLineToPoint:CGPointMake(w, 10)];
    [arrPath addLineToPoint:CGPointMake(w - 10, 10)];
    [path appendPath:arrPath];
    CAShapeLayer *shape = (CAShapeLayer *)self.layer;
    shape.path = path.CGPath;
}

+ (instancetype)menuWithTitles:(NSArray<NSString *> *)titles withHandle:(ClickBlock)block {
    SCMenuView *menu = [[SCMenuView alloc] init];
    menu.clickBlock = block;
    for (int i = 0; i < titles.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 10 + 36 * i, 90, 36);
        button.titleLabel.font = SC_FONT(14);
        button.tag = 1000 + i;
        [button setTitle:titles[i] forState:0];
        [button setTitleColor:SC_TextBlack_Primary forState:0];
        [button addTarget:menu action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [menu addSubview:button];
        if (i > 0) {
            [menu addLineWithFrame:CGRectMake(0, 10 + 36 * i, 90, 0.5)];
        }
    }
    return menu;
}

- (void)show {
   
    UIControl *view = [[UIControl alloc] initWithFrame:CGRectMake(0, 0, kSCScreenWidth, kSCScreenHeight)];
    view.backgroundColor = SC_RGBA(0, 0, 0, 0.5);
    view.alpha = 0;
    [view addTarget:self action:@selector(hideView) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:self];
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:view];
    [UIView animateWithDuration:0.25 animations:^{
        view.alpha = 1;
    }];
}

- (void)hideView {
    [UIView animateWithDuration:0.25 animations:^{
        self.superview.alpha = 0;
    } completion:^(BOOL finished) {
        [self.superview removeFromSuperview];
    }];
}

- (void)buttonClick:(UIButton *)sender {
    self.clickBlock(sender.tag - 1000);
    [self hideView];
}

@end
